<?php
class Model_employers extends CI_Model {
	public function get_manage_current_job(){
		$this->db->select("*");
		$this->db->from('job_ads');
		$this->db->where('is_deleted','NO');
	    $this->db->where('using_employer_id', $this->session->userdata('employer_active_access'));
	    $this->db->where('date_available>=CURDATE()');
	    $this->db->order_by('job_ads_id','DESC');
		return $this->db->get();
	}
	public function get_manage_expired_job(){
		$this->db->select("*");
		$this->db->from('job_ads');
		$this->db->where('is_deleted','NO');
	    $this->db->where('using_employer_id', $this->session->userdata('employer_active_access'));
	    $this->db->where('date_available<CURDATE()');
	    $this->db->order_by('job_ads_id','DESC');
		return $this->db->get();
	}
	public function get_manage_inactive_job(){
		$this->db->select("*");
		$this->db->from('job_ads');
		$this->db->where('is_deleted','NO');
	    $this->db->where('using_employer_id', $this->session->userdata('employer_active_access'));
		$this->db->where('status',0);
	    $this->db->where('date_available>=CURDATE()');
		
	    $this->db->order_by('job_ads_id','DESC');
		return $this->db->get();
	}
	public function update_access_permission($access_module_permissions,$author_id){
		$this->db->where('using_employer_id',$author_id);
		$this->db->where_in('employer_module_permissions_id',$access_module_permissions);
		$this->db->update('employer_module_permissions',array('is_access'=>'YES'));
		return true;
	}
	public function update_not_access_permission($access_module_permissions,$author_id){
		$this->db->where('using_employer_id',$author_id);
		$this->db->where_not_in('employer_module_permissions_id',$access_module_permissions);
		$this->db->update('employer_module_permissions',array('is_access'=>'NO'));
		return true;
	}
	public function get_current_job_apply($employer_id){
		$this->db->select('COUNT(*) as total_current_job_apply');
		$this->db->from('job_seeker_applied_jobs');
		$this -> db -> join('job_ads','job_ads_id=using_job_ads_id');
		$this->db->where('using_employer_id',$employer_id);
		$this->db->where('apply_status','Pending');
		$this->db->group_by('using_employer_id');
		return $this->db->get();
	}
	public function select_job_statistic_filter_by_date($date_from,$date_to,$job_id){
		$this->db->select('*');
		$this->db->from('job_statistics');
		$this->db->where('date_view >=', $date_from);
		$this->db->where('date_view <=', $date_to);
		$this->db->where('using_job_ads_id',$job_id);
		return $this->db->get();
	}
	public function is_logined(){
		if($this->session->userdata('employer_active_access')){
			return TRUE;
		}else{
			return false;
		}
	}
	public function search_jobseeker_registration($education,$native,$foreign,$previos_employer,$job_title,$category,$location,$keyword){
		
		$this -> db->select('job_seeker_id,first_name,last_name,email,phone');
		$this -> db->from('job_seekers');
		$this -> db -> join('resume','resume.using_job_seeker_id=job_seekers.job_seeker_id', 'left');
		$this -> db -> join('work_history','work_history.using_resume_id=resume.resume_id', 'left');
		$this -> db -> join('job_seeker_educations','job_seeker_educations.using_resume_id=resume.resume_id', 'left');
		$this -> db -> join('job_seeker_proficiency_language','job_seeker_proficiency_language.using_resume_id=resume.resume_id', 'left');
		$this -> db -> join('resume_text_editors','resume_text_editors.using_job_seeker_id=job_seekers.job_seeker_id', 'left');
		$this -> db -> join('job_preference_locations','job_preference_locations.using_job_seeker_id=job_seekers.job_seeker_id', 'left');
		$this -> db -> join('job_preference_categories','job_preference_categories.using_job_seeker_id=job_seekers.job_seeker_id', 'left');
		
		
		if($education!=""){
			$this->db->where('using_education_level_id',$education);
		}
		if($native!=""){
			$this->db->where('native_language_using_language_id',$native);
		}
		if($foreign!=""){
			$this->db->where('using_language_id	',$foreign);
		}
		if($previos_employer!=""){
			$this->db->like('work_history.employer_name	',$previos_employer);
		}
		if($job_title!=""){
			$this->db->like('work_history.job_title',$job_title);
			$this->db->like('work_history.job_duties',$job_title);
		}
		if($category!=""){
			$this->db->where('using_job_sub_category_id',$category);
		}
		if($location!=""){
			$this->db->where('using_location_id',$location);
		}
		if($keyword!=""){
			$this->db->like('text_resume',$keyword);
			$this->db->or_like('first_name',$keyword);
			$this->db->or_like('last_name',$keyword);
			$this->db->or_like('text_resume',$keyword);
			$this->db->or_like('job_title',$keyword);
			$this->db->or_like('program_name',$keyword);
		}
		$this->db->group_by('job_seeker_id');
		$this->db->order_by('job_seeker_id','DESC');
		
		return $this->db->get();
	}
	public function select_sub_account_profile($sub_acc_id){
		$this -> db->select(' employer_id, username, email, mw_employers.status, name, logo, website, description, address, phone, fax, logo');
		$this -> db->from('employers');
		$this -> db -> join('companies','companies.using_employer_id=employers.employer_id', 'left');
		$this -> db->where('employer_id',$sub_acc_id);
		return $this->db->get();
	}
	public function is_job_post_by_sub_account($job_apply_id){
		$this->db->select('job_ads_id,employer_id,is_sub_account,using_sub_account_of_employer,using_job_seeker_id,title,contact_persons.name as contact_name,contact_persons.phone as contact_phone,contact_persons.email as contact_email');
		$this -> db->from('job_seeker_applied_jobs');
		$this -> db -> join('job_ads','job_ads.job_ads_id=job_seeker_applied_jobs.using_job_ads_id');
		$this -> db -> join('employers','employers.employer_id=job_ads.using_employer_id');
		//$this -> db -> join('employers','employers.employer_id=job_ads.using_employer_id');
		$this -> db -> join('contact_persons','contact_persons.contact_person_id=job_ads.using_contact_person_id');
		$this -> db->where('job_seeker_applied_job_id',$job_apply_id);
		return $this->db->get();
	}
}